package com.test.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.test.common.util.KeyGenerator;
import com.test.model.Article;
import com.test.model.Reply;
import com.test.model.User;

public class ReplyController extends Controller{
	
	public void save(){
		int floor = 1;
		String article_id = getPara("article_id");
		String id = KeyGenerator.getUUID();
		String content = getPara("content");
		User user = (User) getSessionAttr("user");
		String author = user.get("username");
		String sqlForFloors = "select id from reply where article_id='"+article_id+"'";
		List<Reply> list = Reply.dao.find(sqlForFloors);
		if(list != null && list.size() > 0){
			floor = list.size()+1;
		}
		Reply.dao.set("id",id).set("author",author).set("reply_date",new Date()).set("floor",floor).set("content",content).set("article_id",article_id).save();
		Article.dao.findById(article_id).set("last_reply_date", new Date()).set("last_reply", author).set("reply", floor+1).update();
		setAttr("article_id", article_id);
		render("/article_detail_common.jsp");
	}
}
